package com.util;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class HttpDownloader {

    private URL url = null;
    private File file = null;

    public String downloadBaseFile(String urlStr) {
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            //创建一个url对象
            url = new URL(urlStr);
            //通過url对象，创建一个HttpURLConnection对象（连接）
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            //通过HttpURLConnection对象，得到InputStream
            reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            //使用io流读取文件
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/Music/";
    public String downloadAllFile(String urlStr,String path, String fileName) {

        InputStream inputStream = null;
        try {
            FileUtils fileUtils = new FileUtils();
            if (fileUtils.isFileExist(PATH + fileName)) {
                String realpath = PATH + fileName ;
//                String realpath = "/storage/emulated/0" + path + fileName ;



                System.out.println("realpath===>" + realpath);
                return realpath;
            } else {
                inputStream = getInputStream(urlStr);
                file = fileUtils.write2SDFromInput(PATH, fileName, inputStream);
                if (file == null) {
                    return "文件为空！";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了";
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
        return file.getAbsolutePath();
    }

    public String downloadAllFile2(String urlStr, String path, String fileName) {

        InputStream inputStream = null;
        try {
            FileUtils fileUtils = new FileUtils();
            if (fileUtils.isFileExist(path + fileName)) {
                String realpath = "/storage/emulated/0" + path + fileName ;
                System.out.println("realpath===>" + realpath);
                return realpath;
            } else {
                inputStream = getInputStream(urlStr);
                file = fileUtils.write2SDFromInput(path, fileName, inputStream);
                if (file == null) {
                    return "文件为空！";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了";
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
        return file.getAbsolutePath();
    }
    public String downloadAllFile3(String urlStr, String path, String fileName) {

        InputStream inputStream = null;
        try {
            FileUtils fileUtils = new FileUtils();
            if (fileUtils.isFileExist(path + fileName)) {
                String realpath = "/storage/emulated/0" + path + fileName ;
                System.out.println("realpath===>" + realpath);
                return realpath;
            } else {
                inputStream = getInputStream(urlStr);
//                file = fileUtils.write2SDFromInput(path, fileName, inputStream);
                new DownUtil2(urlStr,path+fileName,0).start();
                /*if (file == null) {
                    return "文件为空！";
                }*/
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了";
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
        return file.getAbsolutePath();
    }

    private InputStream getInputStream(String urlStr)
            throws MalformedURLException, IOException {
        url = new URL(urlStr);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        return inputStream;
    }
}
